package com.ido.otaclient.util;

import android.content.Context;
import android.content.res.Resources;
import android.os.Handler;
import android.os.Looper;
import android.text.Layout;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.TextView;

import androidx.annotation.LayoutRes;

/**
 * Created by lize on 2017/3/30.
 */

public class ViewUtil {

    /**
     * inflater layout
     *
     * @param context
     * @param layoutRes
     * @return
     */
    public static View inflater(Context context, @LayoutRes int layoutRes) {
        return inflater(context, layoutRes, null);
    }

    public static View inflater(Context context, @LayoutRes int layoutRes, ViewGroup root) {
        return inflater(context, layoutRes, root, false);
    }

    public static View inflater(Context context, @LayoutRes int layoutRes, ViewGroup root, boolean attachToRoot) {
        return LayoutInflater.from(context).inflate(layoutRes, root, attachToRoot);
    }

    public static void runOnUiThread(Runnable runnable) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            runnable.run();
        } else {
            new Handler(Looper.getMainLooper()).post(runnable);
        }
    }

    private static long lastClickTime;

    /**
     * 更新时间为当前时间之后延迟addTime, 这样就屏蔽了addTime时间内的事件,但是不包括delayTime时间,
     * 所以实际的延迟时间是(addTime + delayTime)
     * @param addTime 延迟时间
     */
    public static void updateClickTime(long addTime) {
        lastClickTime = System.currentTimeMillis() + addTime;
    }

    public static void updateClickTime() {
        lastClickTime = System.currentTimeMillis();
    }

    public static boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        long timeD = time - lastClickTime;
        if (0 < timeD && timeD < 500) {
            return true;
        }
        lastClickTime = time;
        return false;
    }

    public static boolean isFastDoubleClick(long delayTime) {
        long time = System.currentTimeMillis();
        long timeD = time - lastClickTime;
        if (0 < timeD && timeD < delayTime) {
            return true;
        }
        lastClickTime = time;
        return false;
    }

    /**
     * 判断一个TextView的文字内容有没有被省略
     */
    public static boolean isEllipsize(TextView textView) {
        Layout layout = textView.getLayout();
        if (layout != null) {
            int lines = layout.getLineCount();
            if (lines > 0) {
                int ellipsisCount = layout.getEllipsisCount(lines - 1);
                if (ellipsisCount > 0) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 减小TextView的字体大小
     */
    public static void decreaseTextSize(TextView textView) {
        int textSize = (int) (textView.getTextSize() / Resources.getSystem().getDisplayMetrics().density);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize - 1);
    }

    /**
     * 如果当前TextView的文字被省略了,如果是,则会自动缩小文字大小,直到刚好不被省略
     * 在TextView还未显示的时候调用. 或者直接设置TextView的文字,也会触发addOnGlobalLayoutListener回调.
     */
    public static void suitableTextSize(final TextView textView) {
        textView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                textView.getViewTreeObserver().removeOnPreDrawListener(this);
                if (isEllipsize(textView)) {
                    decreaseTextSize(textView);
                }
                return true;
            }
        });
        textView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (isEllipsize(textView)) {
                    decreaseTextSize(textView);
                } else {
                    textView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }
            }
        });
    }
}
